﻿/********************************************************************************
    Copyright (C) Binod Nepal, Planet Earth Solutions Pvt. Ltd., Kathmandu.
	Released under the terms of the GNU General Public License, GPL, 
	as published by the Free Software Foundation, either version 3 
	of the License, or (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
    See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
***********************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace MixNP.Web.Classifieds.Account
{
    public partial class Profile : MixNP.BusinessLayer.BaseClass
    {
        protected override void OnInit(EventArgs e)
        {
            this.RequiresLogin = true;
            base.OnInit(e);
        }

        private void UploadImage()
        {
            if (ProfileImage.HasFile)
            {
                string filePath = Server.MapPath("~/Classifieds/ProfilePictures/" + User.Identity.Name + System.IO.Path.GetExtension(ProfileImage.FileName));
                ProfileImage.PostedFile.SaveAs(filePath);

                System.Data.DataSet dataSet = new System.Data.DataSet();
                System.IO.Stream fileStream = ProfileImage.PostedFile.InputStream;
                byte[] data = new byte[(int)fileStream.Length];


                System.Drawing.Image image = System.Drawing.Image.FromStream(ProfileImage.PostedFile.InputStream);

                MixNP.BusinessLayer.Profiles.SaveProfilePicture(User.Identity.Name, System.IO.Path.GetExtension(ProfileImage.FileName), Pes.Utility.Conversion.ConvertImageToByteArray(image, System.Drawing.Imaging.ImageFormat.Jpeg));
            }
        }


        protected void SaveButton_Click(object sender, EventArgs e)
        {
            string emailAddress = User.Identity.Name;
            this.UploadImage();

            bool protectProfilePicture = (MixNP.BusinessLayer.Helpers.RequestForm.GetUserControlFormValue("ProtectProfilePictureRadioButtonList", this.Page).Equals("1"));
            string sex = MixNP.BusinessLayer.Helpers.RequestForm.GetUserControlFormValue("SexRadioButtonList", this.Page);
            string userName = MixNP.BusinessLayer.Helpers.RequestForm.GetUserControlFormValue("NameTextBox", this.Page);
            string nickName = MixNP.BusinessLayer.Helpers.RequestForm.GetUserControlFormValue("NickNameTextBox", this.Page);
            string cellNumber = MixNP.BusinessLayer.Helpers.RequestForm.GetUserControlFormValue("CellNumberTextBox", this.Page);
            string contactNumber = MixNP.BusinessLayer.Helpers.RequestForm.GetUserControlFormValue("ContactNumberTextBox", this.Page);
            bool displayCellNumber = (MixNP.BusinessLayer.Helpers.RequestForm.GetUserControlFormValue("DisplayCellNumberRadioButtonList", this.Page).Equals("1"));
            bool displayContactNumbers = (MixNP.BusinessLayer.Helpers.RequestForm.GetUserControlFormValue("DisplayContactNumberRadioButtonList", this.Page).Equals("1"));
            string addressLine1 = MixNP.BusinessLayer.Helpers.RequestForm.GetUserControlFormValue("AddressLine1TextBox", this.Page);
            string addressLine2 = MixNP.BusinessLayer.Helpers.RequestForm.GetUserControlFormValue("AddressLine2TextBox", this.Page);
            string addressLine3 = MixNP.BusinessLayer.Helpers.RequestForm.GetUserControlFormValue("AddressLine3TextBox", this.Page);
            string city = CityDropDownList.SelectedValue;

            if (MixNP.BusinessLayer.Profiles.UpdateProfile(emailAddress, protectProfilePicture, sex, userName, nickName, cellNumber, displayCellNumber, contactNumber, displayContactNumbers, addressLine1, addressLine2, addressLine3, city))
            {
                Response.Redirect("~/dashboard.mix");
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            string token = string.Empty;
            string protectProfilePicture = "0";
            string sex = string.Empty;
            string emailAddress = string.Empty;
            string userName = string.Empty;
            string nickName = string.Empty;
            string cellNumber = string.Empty;
            string contactNumber = string.Empty;
            string displayCellNumber = "0";
            string displayContactNumbers = "0";
            string addressLine1 = string.Empty;
            string addressLine2 = string.Empty;
            string addressLine3 = string.Empty;
            string city = string.Empty;

            using (System.Data.DataTable table = MixNP.BusinessLayer.Profiles.GetProfile(User.Identity.Name))
            {
                if (table.Rows.Count.Equals(1))
                {
                    caption.Visible = false;

                    emailAddress = Pes.Utility.Conversion.TryCastString(table.Rows[0]["EmailAddress"]);

                    if (Pes.Utility.Conversion.TryCastBoolean(table.Rows[0]["ProtectProfilePicture"]).Equals(true))
                    {
                        protectProfilePicture = "1";
                    }

                    sex = Pes.Utility.Conversion.TryCastString(table.Rows[0]["Sex"]);
                    userName = Pes.Utility.Conversion.TryCastString(table.Rows[0]["Name"]);
                    nickName = Pes.Utility.Conversion.TryCastString(table.Rows[0]["NickName"]);
                    cellNumber = Pes.Utility.Conversion.TryCastString(table.Rows[0]["CellNumber"]);

                    if (Pes.Utility.Conversion.TryCastBoolean(table.Rows[0]["DisplayCellNumber"]).Equals(true))
                    {
                        displayCellNumber = "1";
                    }

                    contactNumber = Pes.Utility.Conversion.TryCastString(table.Rows[0]["ContactNumbers"]);

                    if (Pes.Utility.Conversion.TryCastBoolean(table.Rows[0]["DisplayContactNumbers"]).Equals(true))
                    {
                        displayContactNumbers = "1";
                    }

                    addressLine1 = Pes.Utility.Conversion.TryCastString(table.Rows[0]["AddressLine1"]);
                    addressLine2 = Pes.Utility.Conversion.TryCastString(table.Rows[0]["AddressLine2"]);
                    addressLine3 = Pes.Utility.Conversion.TryCastString(table.Rows[0]["AddressLine3"]);
                    city = Pes.Utility.Conversion.TryCastString(table.Rows[0]["City"]);
                    CityDropDownList.SelectedValue = city;


                    token = MixNP.BusinessLayer.Users.GetSaltFromEmailAddress(emailAddress);
                    string filePath = ResolveUrl("~/Handlers/GetProfilePicture.ashx?Path=" + token + "&RequestId=" + Guid.NewGuid().ToString());

                    StringBuilder html = new StringBuilder();
                    html.Append("<ul class=\"bpad8 thumb\">");
                    html.Append("<li>");
                    html.Append("<a href='" + filePath + "'>");
                    html.Append("<img src='" + filePath + "&H=80&W=80' />");
                    html.Append("</a>");
                    html.Append("</li>");
                    html.Append("</ul>");

                    ProfileImageLiteral.Text = html.ToString();
                }
                else
                {
                    userName = MixNP.BusinessLayer.Users.GetNameFromEmailAddress(User.Identity.Name);
                    nickName = MixNP.BusinessLayer.Users.GetNickNameFromEmailAddress(User.Identity.Name);
                }
            }


            this.AddListControl("ProtectProfilePictureRadioButtonList", "Who Can View Your Profile Picture?", "Everyone=0, Only Me=1", protectProfilePicture, true, "If you choose everyone, the other members can view your profile picture. If you choose only me, a default picture will be shown instead.", "", false, "RadioButton List", 100, this.Page, this.placeHolder);
            this.AddListControl("SexRadioButtonList", "Sex", "Male, Female, Third Gender, Other (Not Mentioned Here)", sex, true, "Select Your Gender", "", false, "RadioButton List", 101, this.Page, this.placeHolder);
            this.AddTextBox("NameTextBox", "Your Real Name", "Text", 128, -1, true, "", "textbox wide", "Please provide your real name. This will not be shown to other members.", userName, false, 102, this.Page, this.placeHolder);
            this.AddTextBox("NickNameTextBox", "Your Nick Name", "Text", 32, -1, true, "", "textbox wide", "Provide a screen name for use in this site. For your privacy, we will show your nick name to other members.", nickName, false, 103, this.Page, this.placeHolder);
            this.AddTextBox("CellNumberTextBox", "Your Cell Number", "Integer", 10, -1, true, "", "textbox wide", "Provide your cell number (without codes) to contact you if you chose to display that to the other members.", cellNumber, false, 104, this.Page, this.placeHolder);
            this.AddListControl("DisplayCellNumberRadioButtonList", "Display Your Cell Number to Other Members", "Yes=1, No=0", displayCellNumber, true, "If you choose yes, the other members can directly contact you regarding your ad. If you choose no, they will have to contact you using the contact form.", "", false, "RadioButton List", 105, this.Page, this.placeHolder);
            this.AddListControl("VerifyCellNumberRadioButtonList", "Verify Your Cell Number Now", "Yes=1, No=0", displayCellNumber, true, "If you choose yes, we will send you an SMS with verification code on your mobile. After verifying your cell number, you will have a verified profile on our site. If you choose no, your will have a normal profile.", "", false, "RadioButton List", 105, this.Page, this.placeHolder);
            this.AddTextBox("ContactNumberTextBox", "Your Contact Number", "Text", 10, -1, true, "", "textbox wide", "Provide your alternative landline or mobile contact number (with area code) to contact you if you chose to display that to the other members.", contactNumber, false, 106, this.Page, this.placeHolder);
            this.AddListControl("DisplayContactNumberRadioButtonList", "Display Your Contact Number to Other Members", "Yes=1, No=0", displayContactNumbers, true, "If you choose yes, the other members can directly contact you regarding your ad. If you choose no, they will have to contact you using the contact form.", "", false, "RadioButton List", 107, this.Page, this.placeHolder);
            this.AddTextBox("AddressLine1TextBox", "Address Line1", "Text", 128, -1, true, "", "textbox wide", "", addressLine1, false, 109, this.Page, this.placeHolder);
            this.AddTextBox("AddressLine2TextBox", "Address Line2", "Text", 128, -1, true, "", "textbox wide", "", addressLine2, false, 110, this.Page, this.placeHolder);
            this.AddTextBox("AddressLine3TextBox", "Address Line3", "Text", 128, -1, true, "", "textbox wide", "", addressLine3, false, 111, this.Page, this.placeHolder);
        }

        private void AddTextBox(string id, string label, string type, int maxLength, double maxValue, bool required, string regex, string cssClass, string note, string initialValue, bool isExpression, short tabIndex, System.Web.UI.Page page, Control placeHolder)
        {
            MixNP.Web.ServiceLayer.ControlManager.AddTextBox(id, label, type, maxLength, maxValue, required, regex, cssClass, note, initialValue, isExpression, tabIndex, page, placeHolder);
        }

        private void AddListControl(string id, string label, string list, string initialValue, bool required, string note, string cssClass, bool isExpression, string type, short tabIndex, System.Web.UI.Page page, Control placeHolder)
        {
            MixNP.Web.ServiceLayer.ControlManager.AddListControl(id, label, list, initialValue, required, note, cssClass, isExpression, type, tabIndex, page, placeHolder);
        }

    }
}